/*
 * unittest4.c
 * make a set of random tests
 * to see cover as much of each function as possible.
 * This test will be for getWinners
 * I will exclude invalid input for now.
 * Help was from class examples, badTestDrawCard.c
 * and an example of ramdom testing from a previous class
 * found at http://code.google.com/p/cs362s2013/source/browse/trunk/lutzjo/dominion/unittest1.c
 *  Created on: Jan 25, 2014
 *      Author: Charles_OSU
 */


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "rngs.h"
#include <math.h>

#define DEBUG 0


// prototypes
int  buyCardTest(int randCard, struct gameState *state);

int  buyCardTest (int randCard, struct gameState *state) {

	int numErrors = 0;
	int result = 0;
    int buyAllowed = 0;

	struct gameState gsAfter;
	memcpy(&gsAfter, state, sizeof(struct gameState));
    int turn;

    result = buyCard(randCard, state);

    turn = gsAfter.whoseTurn;
    int cost = getCost(randCard);

    if (gsAfter.numBuys < 0 || gsAfter.supplyCount[randCard] < 1 || gsAfter.coins < cost) { buyAllowed = -1;  }

    assert(result == buyAllowed);

    if (buyAllowed == 0) {
    	gsAfter.phase = 1;
    	gsAfter.discard[turn][gsAfter.discardCount[turn]] = randCard;
    	gsAfter.discardCount[turn]++;
    	gsAfter.supplyCount[randCard]--;
    }

    assert(gsAfter.phase == state->phase);

    assert(gsAfter.discard[turn][gsAfter.discardCount[turn]] == state->discard[turn][state->discardCount[turn]]);

    assert(gsAfter.discardCount[turn] == state->discardCount[turn]);

    assert(gsAfter.supplyCount[randCard] == state->supplyCount[randCard]);

    return numErrors;
} // end getWinnersTest






int main() {

   // Create main game play setup including structs,
  // variables, and any state I need to emulate

  struct gameState gameStateTest;//testing gameStateTest

  int x, y, z;
 // int playersTest[MAX_PLAYERS];
 // int players;  // players
 // int minPlayers=2;
 // int maxPlayers=4;
  int testNums = 0;
  int randCard;
  //int numErrors = 0;
  int k[10] = {adventurer, council_room, feast, gardens, mine,
 	       remodel, smithy, village, baron, great_hall};

  // Set the random number of players
  //players = (int)floor(Random() * maxPlayers);

 // now seed the game
  SelectStream(2);
  PutSeed(3);


  // Print to our screen which tests are being run
  // which will get directed to out .out file later
  // when we add commands to the makefile to redirect
  // the output

  printf("Testing buyCardTest begin\n");
 // printf ("%d RANDOM TESTS.\n", testNums);

  // Additional setup and initialization. Some code
  // borrowed from badTestDrawCard.c

  for (x = 0; x < 3000; x++) {
      for (y = 0; y < sizeof(struct gameState); y++) {
           ((char*)&gameStateTest)[y] = floor(Random() * 256);
         }

      // Set the random number of players
    //   players = (int)floor(Random() * maxPlayers);
     //  gameStateTest.numPlayers = players;

      randCard = floor(Random() * 10);
      int p;
      for (p = 0; p < MAX_PLAYERS; p++) {
            // Setup my players decks and hands
       gameStateTest.deckCount[p] = floor(Random() * MAX_DECK);
       gameStateTest.discardCount[p] = floor(Random() * MAX_DECK);
       gameStateTest.handCount[p] = floor(Random() * MAX_HAND);


      }

//      for(z=0;z<gameStateTest.handCount[players];z++)
//      {
//
//    	  gameStateTest.hand[players][z] = (int)(Random() * MAX_HAND);
//
//      }
//
//      for(z=0;z<gameStateTest.discardCount[players];z++)
//      	  {
//
//         	  gameStateTest.discard[players][z] = (int)(Random() * MAX_HAND);
//
//           }
//
//      for(z=0; z<gameStateTest.deckCount[players]; z++)
//          {
//
//            gameStateTest.deck[players][z] = (int)(Random() * MAX_HAND);
//
//          }
//
      gameStateTest.whoseTurn = floor(Random() * MAX_PLAYERS);
      buyCardTest(randCard, &gameStateTest);


  //    printf ("Initialization and setup completed now running tests for buyCardTest.\n");
     // numErrors += buyCardTest(players, &gameStateTest);
      testNums += 1;
    }


  // If we get to heere it means we kept running and did not crash the program
  // so our tests must have run now let's see how many errors we got.
    printf ("All Tests ran for buyCardTest\n");
    printf ("%d RANDOM TESTS.\n", testNums);



//if (numErrors > 0) { printf("There were %d errors found for buyCardTest. ", numErrors);}
//else { printf("There were no errors found in buyCardTest.");}


return 0;

}  // End main
